using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Core;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Input;

namespace SpaceTrader.WinUI
{
    public partial class Editor : Form
    {
        private GameObject DragObject;
        private List<GameObject> GameObjects;
        private Surface Renderer;

        public Editor()
        {
            InitializeComponent();

            ScreenSetup();

            GameObjects = new List<GameObject>();
            GameObjects.Add(new GameObject(Renderer.Width/2, Renderer.Height/2, "Ship"));

            GameObjects[0].Throttle = 0.0F;

            propertyGrid1.SelectedObjects = GameObjects.ToArray();
            propertyGrid1.SelectedObject = GameObjects[0];

            SdlDotNet.Core.Events.TargetFps = 45;

            SdlDotNet.Core.Events.VideoResize += new EventHandler<VideoResizeEventArgs>(Events_Resize);
            SdlDotNet.Core.Events.Tick += new EventHandler<TickEventArgs>(Events_Tick);
            SdlDotNet.Core.Events.Quit += new EventHandler<QuitEventArgs>(Events_Quit);

            SdlDotNet.Core.Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(Events_KeyboardDown);
        }

        private void Events_KeyboardDown(object sender, KeyboardEventArgs e)
        {
            if (e.Key == Key.Escape)
                Application.Exit();
        }

        private void ScreenSetup()
        {
            Renderer = new Surface(surfaceControl1.Width, surfaceControl1.Height);
        }

        private void UpdateForm()
        {
            Renderer.Fill(Color.Black);
            foreach (GameObject obj in GameObjects)
            {
                Renderer.Blit(obj.Render());
            }

            surfaceControl1.Blit(Renderer);
        }

        private void Events_Tick(object Sender, TickEventArgs e)
        {
            foreach (GameObject obj in GameObjects)
            {
                obj.Update();
            }
        }

        private void Events_Resize(object Sender, VideoResizeEventArgs e)
        {
            Renderer = new Surface(e.Width, e.Height);
        }

        private void Events_Quit(object sender, QuitEventArgs e)
        {
            SdlDotNet.Core.Events.QuitApplication();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(SdlDotNet.Core.Events.Run));
            thread.IsBackground = true;
            thread.Name = "SDL.NET";
            thread.Priority = ThreadPriority.Normal;
            thread.Start();
        }

        private void RenderTimer_Tick(object sender, EventArgs e)
        {
            UpdateForm();
        }

        private void CreateObject_Click(object sender, EventArgs e)
        {
            GameObject obj = new GameObject(0, 0, "Object" + GameObjects.Count.ToString());
            GameObjects.Add(obj);

            propertyGrid1.SelectedObject = obj;
        }

        private void surfaceControl1_MouseClick(object sender, MouseEventArgs e)
        {
            propertyGrid1.SelectedObject = null;

            foreach (GameObject obj in GameObjects)
            {
                if (obj.Hit(e.Location))
                    propertyGrid1.SelectedObject = obj;
            }
        }

        private void surfaceControl1_MouseDown(object sender, MouseEventArgs e)
        {
            DragObject = null;
            foreach (GameObject obj in GameObjects)
            {
                if (obj.Hit(e.Location))
                {
                    propertyGrid1.SelectedObject = obj;
                    DragObject = obj;
                }
            }
        }

        private void surfaceControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if (DragObject != null)
                DragObject = null;
        }

        private void surfaceControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (DragObject != null)
                DragObject.Position = e.Location;
        }
    }
}